//
//  PollingCompleteTableViewController.m
//  OperationPro
//
//  Created by yaxue wang on 2017/1/17.
//  Copyright © 2017年 展鸿科技. All rights reserved.
//

#import "PollingCompleteTableViewController.h"
#import "PollingCompleteTable.h"
#import "UnusualPollingViewController.h"
#import "IdeaCompleteViewController.h"
#import "PollingDetailViewController.h"
@interface PollingCompleteTableViewController ()
@property(nonatomic,strong)NSMutableArray*dataTableAr;
@property(nonatomic)NSInteger index;
@property(nonatomic,strong)NSMutableDictionary *nameDic;

@end

@implementation PollingCompleteTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.nameDic=self.passDic;
    [self.navBar setHidden:YES];
    _dataTableAr=[[NSMutableArray alloc]init];
    //对应的位置
    _index=[[self.nameDic objectForKey:@"order_no"]integerValue];
    
    self.navBar.leftView = nil;
    UIView *narView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, WindowWidth, 64)];
    narView.backgroundColor =[UIColor whiteColor];
    [self.view addSubview:narView];
    
    UIView *backView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, 120, 64)];
    [narView addSubview:backView];
    [backView bk_whenTapped:^{
        UINavigationController *navcontroller = self.navigationController;
        NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:navcontroller.viewControllers];
        
        for (UIViewController *temp in viewControllers)
        {
            if ([temp isKindOfClass:[PollingDetailViewController class]])
            {
                [self.navigationController popToViewController:temp animated:YES];
            }
        }
    }];
    
    UIImage *icoBack = [UIImage imageNamed:@"Ico_nar_back"];
    
    UIImageView *imgBack = [[UIImageView alloc] initWithFrame:CGRectMake(15, 33, icoBack.size.width, icoBack.size.height)];
    imgBack.image=icoBack;
    [backView addSubview:imgBack];
    
    UILabel *backTitle = [[UILabel alloc] init];
    backTitle.frame=CGRectMake(CGRectGetMaxX(imgBack.frame) + 5, 20, 70,44);
    [backTitle setText:@"巡检详情"];
    [backTitle setTextColor:MainColor];
    [backTitle setFont:F(14)];
    [backView addSubview:backTitle];

    
    UILabel*titleLab =[[UILabel alloc]init];
    [narView addSubview:titleLab];
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(narView);
        make.top.equalTo(narView).offset(30);
    }];
    titleLab.font =F(16);
    titleLab.textColor =C(@"5a5a5a");
    titleLab.text=[self.nameDic objectForKey:@"name"];
    
    PollingCompleteTable *networkView=[[PollingCompleteTable alloc]initWithFrame:CGRectMake(0, 74, WindowWidth, WindowHeight-74) withUuid:self.Uuid WithItemUuid:[self.nameDic objectForKey:@"uuid"] withIndex:_index withUnusualState:self.unusalState];
    
    [self.view addSubview:networkView];
    
    //上个界面
    [networkView setGotoUpward:^(NSString *string) {
        [self gotoUp];
    }];
    //下个界面
    [networkView setGotoDownward:^(NSString *string) {
        
        [self gotoNext];
        
    }];
    
}
#pragma mark 下一页
- (void)gotoNext
{
    
    _index++;
    if (_index==0) {
        //返回到表格列表
        
    }else if(_index>self.allAr.count)
    {
        
        IdeaCompleteViewController *ideaVC=[IdeaCompleteViewController new];
        NSMutableDictionary *dic=[self.allAr objectAtIndex:_index-2];
        NSMutableDictionary *dataDic=[[NSMutableDictionary alloc]init];
        [dataDic setObject:[dic objectForKey:@"name"] forKey:@"name"];
        [dataDic setObject:[dic objectForKey:@"uuid"] forKey:@"uuid"];
        [dataDic setObject:[NSString stringWithFormat:@"%d",[[dic objectForKey:@"order_no"] integerValue]] forKey:@"order_no"];
        ideaVC.passDic=dataDic;
        ideaVC.allAr=self.allAr;
        ideaVC.Uuid=self.Uuid;
        ideaVC.unusualState=self.unusalState;
        [self pushController:ideaVC];
        
        
    }else
    {
        for (int i=0; i<self.allAr.count; i++) {
            NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
            dic=[self.allAr objectAtIndex:i];
            if (_index==[[dic objectForKey:@"order_no"] integerValue]) {
                PollingCompleteTableViewController *pollingVC=[PollingCompleteTableViewController new];
                NSMutableDictionary *dic=[self.allAr objectAtIndex:_index-1];
                NSMutableDictionary *dataDic=[[NSMutableDictionary alloc]init];
                [dataDic setObject:[dic objectForKey:@"name"] forKey:@"name"];
                [dataDic setObject:[dic objectForKey:@"uuid"] forKey:@"uuid"];
                [dataDic setObject:[NSString stringWithFormat:@"%d",[[dic objectForKey:@"order_no"] integerValue]] forKey:@"order_no"];
                pollingVC.passDic=dataDic;
                pollingVC.allAr=self.allAr;
                pollingVC.Uuid=self.Uuid;
                pollingVC.unusalState=self.unusalState;
                [self pushController:pollingVC];
                return ;
                
            }
        }
        
    }
    
    
}
#pragma mark  上一页
- (void)gotoUp
{
    _index--;
    if (_index==0) {
        if ([self.unusalState isEqualToString:@"1"]) {
            //返回异常
            UnusualPollingViewController *unusualVC=[[UnusualPollingViewController alloc]init];
            NSMutableDictionary *dic=[self.allAr objectAtIndex:0];
            NSMutableDictionary *dataDic=[[NSMutableDictionary alloc]init];
            [dataDic setObject:[dic objectForKey:@"name"] forKey:@"name"];
            [dataDic setObject:[dic objectForKey:@"uuid"] forKey:@"uuid"];
            [dataDic setObject:[NSString stringWithFormat:@"%d",[[dic objectForKey:@"order_no"] integerValue]] forKey:@"order_no"];
            unusualVC.passDic=dataDic;
            unusualVC.allAr=self.allAr;
            unusualVC.unusualState=self.unusalState;
            unusualVC.Uuid=self.Uuid;
            [self pushController:unusualVC Lever:0 Animated:NO];
        }
       
    }else
    {
        for (int i=0; i<self.allAr.count; i++) {
            NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
            dic=[self.allAr objectAtIndex:i];
            if (_index==[[dic objectForKey:@"order_no"] integerValue]) {
                PollingCompleteTableViewController *pollingVC=[PollingCompleteTableViewController new];
                NSMutableDictionary *dic=[self.allAr objectAtIndex:_index-1];
                NSMutableDictionary *dataDic=[[NSMutableDictionary alloc]init];
                [dataDic setObject:[dic objectForKey:@"name"] forKey:@"name"];
                [dataDic setObject:[dic objectForKey:@"uuid"] forKey:@"uuid"];
                [dataDic setObject:[NSString stringWithFormat:@"%d",[[dic objectForKey:@"order_no"] integerValue]] forKey:@"order_no"];
                
                pollingVC.passDic=dataDic;
                pollingVC.allAr=self.allAr;
                pollingVC.Uuid=self.Uuid;
                pollingVC.unusalState=self.unusalState;

                [self pushController:pollingVC Lever:0 Animated:NO];
                
                
                return ;
                
            }
            
        }
        
        
    }
    
}




@end
